//
//  HDFontView.swift
//  Wmsmobile
//
//  Created by wangshuning on 16/9/20.
//  Copyright © 2016年 www.hd123.com. All rights reserved.
//

import UIKit

class HDFontView: UIImageView , UIPickerViewDataSource , UIPickerViewDelegate{
    
    //MARK: - lifeCycle
    var dataArr: [String] = ["默认" , "中号" , "大号"]
    var string : String?
    
    var closeSure: ((Int) -> ())?
    lazy fileprivate var pickerView:UIPickerView = {
        let picker = UIPickerView(frame: CGRect(x: 0, y: 0, width: KscreenWidth, height: 207))
        picker.backgroundColor = UIColor.white
        return picker
    }()
    lazy fileprivate var sepectorView:UIView = {
        let sepectorView = UIView(frame: CGRect(x: 0, y: 83, width: KscreenWidth , height: 0))
        sepectorView.backgroundColor = UIColor.hexStringToUIColor("#979797")
        return sepectorView
    }()
    lazy fileprivate var maskview:UIView = {
        let maskview = UIView(frame: CGRect(x: 0, y: 0, width: KscreenWidth, height: KscreenHeight-64))
        maskview.backgroundColor = UIColor.black
        maskview.alpha = 0.5
        return maskview
    }()
    
    init(dataArray: [[String : String]]){
        super.init(frame: CGRect(x: 0, y: KscreenHeight - 207, width: KscreenWidth, height: 207))
        self.backgroundColor = UIColor.green
        self.pickerView.dataSource = self
        self.pickerView.delegate = self
        self.addSubview(pickerView)
        let lbutton = UIButton(frame: CGRect(x: 0, y:0, width: KscreenWidth/4+5, height: 40))
        lbutton.setTitle("取消", for: UIControlState())
        lbutton.setTitleColor(UIColor.black, for: UIControlState())
        lbutton.addTarget(self, action:  #selector(SelectTableView.pickerCacale(_:)), for: UIControlEvents.touchUpInside)
        lbutton.backgroundColor = UIColor.hexStringToUIColor("#e5e5e5")
        self.addSubview(lbutton)
        
        let label = UILabel(frame: CGRect(x: KscreenWidth/4, y: 0, width: KscreenWidth/2, height: 40))
        label.backgroundColor = UIColor.hexStringToUIColor("#e5e5e5")
        label.text = "选择字体"
        label.textAlignment = NSTextAlignment.center
        self.addSubview(label)
        
        let rbutton = UIButton(frame: CGRect(x: KscreenWidth - KscreenWidth/4, y:0, width: KscreenWidth/3, height: 40))
        rbutton.setTitle("确定", for: UIControlState())
        rbutton.setTitleColor(UIColor.black, for: UIControlState())
        rbutton.addTarget(self, action:  #selector(SelectTableView.pickerDone(_:)), for: UIControlEvents.touchUpInside)
        rbutton.backgroundColor = UIColor.hexStringToUIColor("#e5e5e5")
        self.addSubview(rbutton)
        
        self.image =  UIImage(named: "")
        self.backgroundColor = UIColor.clear
        self.addSubview(sepectorView)
        self.isUserInteractionEnabled = true
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func pickerDone(_ picker : UIPickerView){
        
        if let closeSure = self.closeSure{
            closeSure(self.pickerView.selectedRow(inComponent: 0))
        }
        self.removeFromSuperview()
        maskview.removeFromSuperview()
    }
    func pickerCacale(_ picker : UIPickerView){
        self.removeFromSuperview()
        maskview.removeFromSuperview()
        
    }
    //MARK: - tableView delegate
   
    func pickerView(_ pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
        return 44
    }
    //MARK: - tableView DataSource
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return 3
    }

    func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
        
        let label = UILabel()
        label.frame = CGRect(x: 0, y: 0, width: 150, height: 40)
        label.textAlignment = .center
        label.text = dataArr[row]
        return label
    }
    func showSelectorHD(_ onView:UIView, seletedRow:Int , success:@escaping (Int) -> ()){
        let window = UIApplication.shared.keyWindow
        window!.addSubview(maskview)
        window!.addSubview(self)
        self.pickerView.selectRow(seletedRow, inComponent: 0, animated: true)
        self.closeSure = success
    }
    
}
//MARK: - 用来展示picker中的view
class labelView: UIView {
    lazy fileprivate var labelLeft:UILabel = {
        let label = UILabel()
        label.textColor = UIColor.black
        label.isHidden = true
        label.font = UIFont.systemFont(ofSize: 17)
        return label
    }()
    lazy fileprivate var labelRight:UILabel = {
        let label = UILabel()
        label.textColor = UIColor.black
        label.font = UIFont.systemFont(ofSize: 17)
        return label
    }()
    
    init(textLeft: String, textRight:String) {
        super.init(frame: CGRect(x: 0, y: 0, width: 200, height: 40))
        labelLeft.text = textLeft
//        labelLeft.backgroundColor = UIColor.blueColor()
//        labelRight.backgroundColor = UIColor.greenColor()
        labelRight.text = textRight
        self.addSubview(labelLeft)
        self.addSubview(labelRight)
        labelLeft.snp.makeConstraints { (make) in
            make.centerX.equalTo(self)
            make.centerY.equalTo(self)
        }
        labelRight.snp.makeConstraints { (make) in
            make.centerX.equalTo(self)
            make.centerY.equalTo(self)
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
